﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using PNProject.Core;
using PNProject.EF;
using System.Globalization;

namespace PNProject.Controllers
{
    public class ProductsController : Controller
    {
        public ActionResult Index(int? id)
        {
            var member = Membership.GetUser();
            if (member != null)
            {
                var userName = member.UserName;
                Utils.TrackingUser(null, userName, "ProductsController", "Index", Request.Url.PathAndQuery.ToString());
            }
            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            if (id.HasValue)
            {
                var pro = StaticData.GetAllProductByCatalog(id.Value);
                var cate = StaticData.cateRepo.GetOne(a => a.CategoryID == id);
                if (lang)
                    ViewData["caname"] = cate.CategoryName;
                else
                    ViewData["caname"] = cate.CategoryNameEN;
                 ViewData["caid"] = cate.CategoryID;
                return View(pro);
            }
            ViewData["caname"] = "";
            ViewData["caid"] = 0;
            var products = StaticData.GetAllProduct();
            return View(products);
        }
        public ActionResult ItemProduct(int id)
        {
            var member = Membership.GetUser();
            if (member != null)
            {
                var userName = member.UserName;
                Utils.TrackingUser(null, userName, "ProductsController", "ItemProduct", Request.Url.PathAndQuery.ToString());
            }
            var products = StaticData.GetAllProductByCatalog(id);
            return View("Index", products);
        }
        public ActionResult SubmenuProductAndServices()
        {
            var list = StaticData.cateRepo.GetAll();
            return PartialView(list);
        }
        public ActionResult SubmenuProductAndServices1()
        {
            var list = StaticData.cateRepo.GetAll();
            return PartialView(list);
        }
        
        public ActionResult SearchProduct(string key)
        {
            var member = Membership.GetUser();
            if (member != null)
            {
                var userName = member.UserName;
                Utils.TrackingUser(null, userName, "ProductsController", "SearchProduct", Request.Url.PathAndQuery.ToString());
            }
            bool lang = CultureInfo.CurrentUICulture.Name == "vi-VN" ? true : false;
            var products = StaticData.proRepo.GetSome(a => a.Name.Contains(key) && a.Lang == lang).OrderByDescending(a => a.LastUpdate);
            //var products = StaticData.GetAllProduct();
            //if (products.Any())
            //    products = products.Where(a => a.Name.ToUpperInvariant().Contains(key.ToUpper())).OrderByDescending(b=>b.CreateDate);
            return View("Index",products);
        }
        
    }
}
